﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TracNghiem.Areas.Admin.Models;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TracNghiem.Areas.Admin.Controllers
{
    [Authorize(Roles = "admin")]
    public class ReportsController : Controller
    {
        //
        // GET: /Admin/Reports/
        ReportsModel ReportsModel = new ReportsModel();
        DichvuModel DichvuModel = new DichvuModel();
        TestModel TestModel = new TestModel();

        public ActionResult Index()
        {
            //List<DichvuModel> DichvuModel_ = DichvuModel.getDichvu();
            //ViewBag.dichvu_id = new SelectList(DichvuModel_, "id", "name");

            List<ReportsModel> report = ReportsModel.getReport_Question();
            report_global = report;
            ViewBag.theanh = TracNghiem.Models.General.Get_select();
            return View(report);
        }
        [HttpPost]
        public ActionResult FindReports(string timkiem)
        {
            string dichvu_id = "";
            int num;
            string s = "";
            ViewBag.selectedService = "";
            if (int.TryParse(Request.Form["service_id"], out num))
            {
                dichvu_id = Request.Form["service_id"];
                s = " where dichvu_id=" + dichvu_id;
                ViewBag.selectedService = Request.Form["service_id"];
            }
            List<ReportsModel> report = ReportsModel.FindReport_Question(s);
            List<DichvuModel> Dichvu = DichvuModel.getDichvu();
            if (ViewBag.selectedService.Length == 0)
                ViewBag.dichvu_id = new SelectList(Dichvu, "id", "name");
            else
            {
                ViewBag.dichvu_id = new SelectList(Dichvu, "id", "name", ViewBag.selectedService);
                
            }

            
            ViewBag.theanh = TracNghiem.Models.General.Get_select(Request.Form["service_id"]);
            

            report_global = report;
            return View("Index", report);
        }
        public static List<ReportsModel> report_global { get; set; }
        public void Export_Excel()
        {
            var item = new System.Data.DataTable();
            item.Columns.Add("Tên dịch vụ", typeof(string));
            item.Columns.Add("Phân loại", typeof(string));
            item.Columns.Add("Câu hỏi", typeof(string));
            item.Columns.Add("Số lần đúng", typeof(string));
            item.Columns.Add("Số lần thi", typeof(string));
            item.Columns.Add("Tỷ lệ", typeof(string));
            for (int i = 0; i < report_global.ToArray().Length; i++)
                item.Rows.Add(report_global[i].ten_dich_vu, report_global[i].ten_phan_loai, report_global[i].name, report_global[i].cau_dung, report_global[i].tong, Math.Round(Convert.ToDecimal(report_global[i].cau_dung) / Convert.ToDecimal(report_global[i].tong),2));
            var grid = new GridView();
            grid.DataSource = item;
            grid.DataBind();

            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=bao_cao_cau_hoi.xls");
            Response.ContentType = "application/ms-excel";
            Response.ContentEncoding = System.Text.Encoding.Unicode;
            Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
            //Response.ContentType = "application/openxmlformats";
            //Response.AddHeader("Content-Disposition", "attachment; filename="+"bao_cao.xlsx");


            Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            grid.RenderControl(htw);

            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }
    }
}
